package com.hod.client.charge;

import com.google.gwt.gears.client.database.DatabaseException;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.DateFieldDef;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.FloatFieldDef;
import com.gwtext.client.data.IntegerFieldDef;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.grid.BaseColumnConfig;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtextux.client.data.PagingMemoryProxy;
import com.hod.client.backend.orm.client.GearsDataStore;
import com.hod.client.backend.orm.client.GearsQuery;
import com.hod.client.frontend.AbstractGrid;

public class ChargeGrid extends AbstractGrid {
	private static RecordDef recordDef = new RecordDef(
            new FieldDef[]{
                    new IntegerFieldDef("idLodging"),
                    new IntegerFieldDef("idCharge"),                    
                    new IntegerFieldDef("idChargeDescription"),
                    new DateFieldDef("date"),
                    new FloatFieldDef("total")                    
               }
    );
	@Override
	protected void getData() {
		getData("");

	}

	@Override
	protected void getData(String idLodging) {
		// TODO Auto-generated method stub
		PagingMemoryProxy proxy = null;
		try {			
			GearsQuery q = null;
			if (idLodging.length()>0){				
				q = this.ds.getNamedQuery(Charge.FIND_ALL_BY_ID);
				q.setString(0, idLodging);
			}else{
				q = this.ds.getNamedQuery(Charge.FIND_ALL);
			}
			Charge[] data = (Charge[]) q.execute();
			Charge rs = null;
			Object[][]data2 = new Object[data.length][];
			for (int i = 0; i < data.length; i++) {
                rs = data[i];	               
                data2[i] = new Object[]{rs.getIdLodging(),rs.getIdCharge(),rs.getIdChargeDescription(),rs.getDate(),rs.getTotal()};
            }
			proxy = new PagingMemoryProxy(data2);
			
			
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			//Window.alert(e.getMessage());
		} 

        ArrayReader reader = new ArrayReader(recordDef);
        store = new Store(proxy, reader);
        store.load(0,numberRecords);
        setStore(store);
	}

	public ChargeGrid(boolean small,GearsDataStore ds,boolean getData) {
		this.ds = ds;
    	columns = new BaseColumnConfig[]{
                new ColumnConfig("idLodging", "idLodging", 60, true, null, "idLodging"),    	
                new ColumnConfig("idCharge", "idCharge", 60, true, null, "idCharge"),
                new ColumnConfig("idChargeDescription", "idChargeDescription", 60, true, null, "idChargeDescription"),
                new ColumnConfig("date", "date", 60, true, null, "date"),
                new ColumnConfig("total", "total", 60, true, null, "total")
        };
    	
    	getData();

        ColumnModel columnModel = getColumnConfigs();
        setColumnModel(columnModel);

        setFrame(true);
        setStripeRows(true);
        setIconCls("grid-icon");      

        createPopUp();
        createPager(); 
	}

}
